﻿Public Class CIRREGULARState : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CIRREGULARState.IsEnter")
        If CCommon.isInStr(task.ActionCd, "IRREGULAR") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase
        Dim pdaTask As CPDATask = DirectCast(task, CPDATask)
        Debug.WriteLine("CIRREGULARState.DoExecute")
        Dim readText As String = task.ProcessText

        If pdaTask.checkParentLabel(CCommon.LABEL_DNC) Then ''ダミーでDNCラベルを入れる

            If pdaTask.chkOutQty() Then
                ''入り数チェック
                ''正常
                If pdaTask.commitLabel() Then
                    ''正常
                Else
                    ''error(通常ありえない）
                    'pdaTask.addWindowLabelBox(CCommon.LABEL_DNC, 2)
                    pdaTask.rollbackLabel()
                    'エラー
                    pdaTask.Hardware.warning()
                    ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

                    SyncLock pdaTask.m_SendLockObject
                        Try
                            pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, CCommon.LABEL_DNC, "", "", "", "", 0))
                            pdaTask.C_FileCommon.SendTrxOrder()

                        Catch ex As Exception

                        End Try
                    End SyncLock

                    Dim result As String = ""
                    Dim prevWindowMode As EnumWindowMode
                    Do While result <> "[OK]"
                        If result = "" Then
                            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_016, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                        End If
                        result = task.ReadTextWait(-1)
                        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                    Loop
                    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)



                    task.ErrorCd = "ER"
                    task.ProcessText = ""
                    nextState = GetNextState(task, GetType(CErrorState))
                    Return nextState

                End If
            Else
                ''入り数チェック
                ''エラー
                ''error
                'pdaTask.addWindowLabelBox(CCommon.LABEL_DNC, 2)
                pdaTask.rollbackLabel()
                'エラー
                pdaTask.Hardware.warning()
                ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

                SyncLock pdaTask.m_SendLockObject
                    Try
                        pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, CCommon.LABEL_DNC, "", "", "", "", 0))
                        pdaTask.C_FileCommon.SendTrxOrder()

                    Catch ex As Exception

                    End Try
                End SyncLock


                Dim result As String = ""
                Dim prevWindowMode As EnumWindowMode
                Do While result <> "[OK]"
                    If result = "" Then
                        prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                        pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_025, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                        pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                    End If
                    result = task.ReadTextWait(-1)
                    'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                Loop
                pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)



                task.ErrorCd = "ER"
                task.ProcessText = ""
                nextState = GetNextState(task, GetType(CErrorState))
                Return nextState


            End If
        End If



        Dim stateStatus As Integer = 0

        Select Case readText
            Case CCommon.CNS_RESET2 ''注文分割
                'stateStatus = 1
                'task.ProcessText = ""

                'Do While CBarcode.GetBarcodeType(task.ProcessText) <> EnumBarcodeType.OUTER_LABEL_CODE_D
                '    If task.ProcessText <> "" Then
                '        Dim result As String = ""
                '        Dim prevWindowMode As EnumWindowMode
                '        Do While result <> "[OK]"
                '            If result = "" Then
                '                pdaTask.Hardware.alert()
                '                prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                '                pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_046, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                '                pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)

                '                task.ActionCd = "LABEL"
                '                task.ProcessText = ""
                '                nextState = GetNextState(task)
                '                Return nextState

                '            End If
                '            result = task.ReadTextWait(-1)

                '        Loop
                '        pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)

                '    End If
                '    '' リーダーからデータを取得する(待ち時間制限なし)
                '    pdaTask.WindowManager.SetMessage(CCommon.CNS_MSG_049, CCommon.COLOR_BLACK, CCommon.COLOR_GREEN)
                '    task.ProcessText = task.ReadTextWait(-1)
                'Loop
                'readText = task.ProcessText ''DNCラベルコード


                'SyncLock pdaTask.m_SendLockObject

                '    Dim ReportCd As String = ""
                '    Dim RowNum As String = ""
                '    Dim OrderCd As String = CCommon.TrimCode(readText, CCommon.EnumGetString.LBL_D_ORDER_CD)
                '    Dim SysItemCd As String = ""
                '    Dim itemKey As String = CCommon.TrimCode(readText, CCommon.EnumGetString.LBL_D_ITEM_KEY)

                '    Dim actionData As typActionOrderRecord = Nothing

                '    If pdaTask.deleteOrderData(OrderCd, ReportCd, RowNum, actionData) Then
                '        ''正常
                '        ''ユニークコードが存在すれば削除する
                '        ''DAO
                '        Dim itemDAO As CItemDAO = CItemDAO.GetInstance()
                '        Dim itemData As strctItemInfo = itemDAO.GetItemInfo(itemKey)
                '        If itemData.MngNo <> "" Then
                '            SysItemCd = itemData.SysItemCd
                '        End If

                '        Dim uniqueCdInfo As CPDATask.strctUniqueCdInfo = Nothing
                '        uniqueCdInfo.labelType = CCommon.LABEL_DNC
                '        uniqueCdInfo.itemCd = ""
                '        uniqueCdInfo.uniqueCd = readText
                '        uniqueCdInfo.readDateTime = Now()
                '        pdaTask.isInUniqueCd(uniqueCdInfo)

                '        ''取消情報を送信
                '        Try
                '            pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendCancelData(ReportCd, RowNum, OrderCd, SysItemCd, actionData))
                '            pdaTask.C_FileCommon.SendTrxOrder()

                '        Catch ex As Exception

                '        End Try

                '    Else
                '        ''警告　データが存在しない
                '        ''アラート
                '        Dim result As String = ""
                '        Dim prevWindowMode As EnumWindowMode
                '        Do While result <> "[OK]"
                '            If result = "" Then
                '                pdaTask.Hardware.alert()
                '                prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                '                pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_047, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                '                pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                '                'Debug.WriteLine(CCommon.CNS_MSG_015)

                '            End If
                '            result = task.ReadTextWait(-1)
                '            'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                '        Loop
                '        pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)

                '    End If
                'End SyncLock


            Case CCommon.CNS_RESET3 ''注文変更
                stateStatus = 2
                task.ProcessText = ""

                Do While CBarcode.GetBarcodeType(task.ProcessText) <> EnumBarcodeType.OUTER_LABEL_CODE_D
                    If task.ProcessText <> "" Then
                        Dim result As String = ""
                        Dim prevWindowMode As EnumWindowMode
                        Do While result <> "[OK]"
                            If result = "" Then
                                pdaTask.Hardware.alert()
                                prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                                pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_046, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                                pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)


                            End If
                            result = task.ReadTextWait(-1)

                        Loop
                        pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)

                        task.ActionCd = "LABEL"
                        task.ProcessText = ""
                        nextState = GetNextState(task)
                        Return nextState

                    End If
                    '' リーダーからデータを取得する(待ち時間制限なし)
                    pdaTask.WindowManager.SetMessage(CCommon.CNS_MSG_048, CCommon.COLOR_BLACK, CCommon.COLOR_GREEN)
                    task.ProcessText = task.ReadTextWait(-1)
                Loop
                readText = task.ProcessText ''DNCラベルコード

                SyncLock pdaTask.m_SendLockObject

                    Dim ReportCd As String = ""
                    Dim RowNum As String = ""
                    Dim OrderCd As String = CCommon.TrimCode(readText, CCommon.EnumGetString.LBL_D_ORDER_CD)
                    Dim OrderQty As Integer = CCommon.From36To10(CCommon.TrimCode(readText, CCommon.EnumGetString.LBL_D_ORDER_QTY))
                    Dim SysItemCd As String = ""
                    Dim itemKey As String = CCommon.TrimCode(readText, CCommon.EnumGetString.LBL_D_ITEM_KEY)


                    If pdaTask.changeOrderData(readText, OrderCd, OrderQty, ReportCd, RowNum) Then
                        ''正常
                        ''ユニークコードが存在すれば削除する
                        ''DAO
                        Dim itemDAO As CItemDAO = CItemDAO.GetInstance()
                        Dim itemData As strctItemInfo = itemDAO.GetItemInfo(itemKey)
                        If itemData.MngNo <> "" Then
                            SysItemCd = itemData.SysItemCd
                        End If

                        Dim uniqueCdInfo As CPDATask.strctUniqueCdInfo = Nothing
                        uniqueCdInfo.labelType = CCommon.LABEL_DNC
                        uniqueCdInfo.itemCd = ""
                        uniqueCdInfo.uniqueCd = readText
                        uniqueCdInfo.readDateTime = Now()
                        pdaTask.isInUniqueCd(uniqueCdInfo)

                        ''取消情報を送信
                        Try
                            pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendCancelData(ReportCd, RowNum, OrderCd, SysItemCd))
                            pdaTask.C_FileCommon.SendTrxOrder()

                        Catch ex As Exception

                        End Try

                    Else
                        stateStatus = 0
                        ''警告　データが存在しない
                        ''アラート
                        Dim result As String = ""
                        Dim prevWindowMode As EnumWindowMode
                        Do While result <> "[OK]"
                            If result = "" Then
                                pdaTask.Hardware.alert()
                                prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                                pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_047, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                                pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                                'Debug.WriteLine(CCommon.CNS_MSG_015)

                            End If
                            result = task.ReadTextWait(-1)
                        Loop
                        pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)

                    End If
                End SyncLock
            Case CCommon.CNS_RESET4 ''注文取消
                stateStatus = 3
                task.ProcessText = ""

                Do While CBarcode.GetBarcodeType(task.ProcessText) <> EnumBarcodeType.OUTER_LABEL_CODE_D
                    If task.ProcessText <> "" Then
                        Dim result As String = ""
                        Dim prevWindowMode As EnumWindowMode
                        Do While result <> "[OK]"
                            If result = "" Then
                                pdaTask.Hardware.alert()
                                prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                                pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_046, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                                pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)


                            End If
                            result = task.ReadTextWait(-1)

                        Loop
                        pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)

                        task.ActionCd = "LABEL"
                        task.ProcessText = ""
                        nextState = GetNextState(task)
                        Return nextState

                    End If
                    '' リーダーからデータを取得する(待ち時間制限なし)
                    pdaTask.WindowManager.SetMessage(CCommon.CNS_MSG_045, CCommon.COLOR_BLACK, CCommon.COLOR_GREEN)
                    task.ProcessText = task.ReadTextWait(-1)
                Loop
                readText = task.ProcessText ''DNCラベルコード

                SyncLock pdaTask.m_SendLockObject

                    Dim ReportCd As String = ""
                    Dim RowNum As String = ""
                    Dim OrderCd As String = CCommon.TrimCode(readText, CCommon.EnumGetString.LBL_D_ORDER_CD)
                    Dim SysItemCd As String = ""
                    Dim itemKey As String = CCommon.TrimCode(readText, CCommon.EnumGetString.LBL_D_ITEM_KEY)

                    Dim actionData As typActionOrderRecord = Nothing

                    If pdaTask.deleteOrderData(OrderCd, ReportCd, RowNum, actionData) Then
                        ''正常
                        ''ユニークコードが存在すれば削除する
                        ''DAO
                        Dim itemDAO As CItemDAO = CItemDAO.GetInstance()
                        Dim itemData As strctItemInfo = itemDAO.GetItemInfo(itemKey)
                        If itemData.MngNo <> "" Then
                            SysItemCd = itemData.SysItemCd
                        End If

                        Dim uniqueCdInfo As CPDATask.strctUniqueCdInfo = Nothing
                        uniqueCdInfo.labelType = CCommon.LABEL_DNC
                        uniqueCdInfo.itemCd = ""
                        uniqueCdInfo.uniqueCd = readText
                        uniqueCdInfo.readDateTime = Now()
                        pdaTask.isInUniqueCd(uniqueCdInfo)

                        ''取消情報を送信
                        Try
                            pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendCancelData(ReportCd, RowNum, OrderCd, SysItemCd, actionData))
                            pdaTask.C_FileCommon.SendTrxOrder()

                        Catch ex As Exception

                        End Try

                    Else
                        ''警告　データが存在しない
                        ''アラート
                        Dim result As String = ""
                        Dim prevWindowMode As EnumWindowMode
                        Do While result <> "[OK]"
                            If result = "" Then
                                pdaTask.Hardware.alert()
                                prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                                pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_047, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                                pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                                'Debug.WriteLine(CCommon.CNS_MSG_015)

                            End If
                            result = task.ReadTextWait(-1)
                            'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                        Loop
                        pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)

                    End If
                End SyncLock


        End Select

        'If stateStatus = 2 Then
        '    task.ActionCd = "LABEL-D"
        'Else
        task.ActionCd = "LABEL"
        task.ProcessText = ""
        'End If
        nextState = GetNextState(task)
        Return nextState
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CIRREGULARState.DoExit")
        Return True
    End Function

End Class
